﻿using AjaxPro;
using System;
using System.Collections.Generic;
using System.Data;
using System.Web.UI;
using WorkFlow.DBProxy;
using WorkFlow.Engine;
using WorkFlow.Engine.NSMessage;

namespace WorkFlow.web
{
    /// <summary>
    ///     通知区域 在上面显示代办任务列表
    /// </summary>
    [AjaxNamespace("nsmsg")]
    public partial class NotificationPanel : Page
    {
        /// <summary>
        ///     根据用户ID
        ///     获取代办事件列表
        /// </summary>
        [AjaxMethod]
        public DataTable GetCaseList()
        {
            var fc = new WfCase();
            int userID;
            if (!int.TryParse(Session["USERID"].ToString(), out userID)) return null;
            List<string> list = fc.GetCasesByUser(userID);
            int count = list == null ? 0 : list.Count;
            if (count == 0) return null;

            string str = "(" + list[0];
            for (int i = 1; i < count; i++)
            {
                str += "," + list[i];
            }
            str += ")";

            string sql = string.Format(@"select c.f_id AS CID,c.f_title AS CTT,T.f_id as AID
                                                              from t_cases c,t_activities t ,t_processes p
                                                                where c.f_id=p.f_case_id
                                                                and t.f_process_id=p.f_id
                                                                and t.f_id in {0} ", str);
            return DB.MapToDataTable(sql);
        }

        /// <summary>
        ///     打开新页面
        ///     获取新页面数据
        /// </summary>
        /// <param name="ids"></param>
        [AjaxMethod]
        public DataTable OpenNewPage(string aid)
        {
            int actID;
            if (!int.TryParse(aid, out actID)) return null;

            string sql =
                string.Format(@"select f.f_url as URL,a.f_id as ACTID  from tr_act_user au
                                                inner join t_activities a on au.f_act_id =a.f_r_act_id and
                                                a.f_id ={0} and a.f_status in (1,2,3,4)
                                                inner join T_MODULES f on f.f_id= au.f_module_id", actID);

            DataTable dt = DB.MapToDataTable(sql);
            //如果存在数据,获取一次活动后即可更新 Activitycontroller里面的数据
            if (dt != null && dt.Rows.Count > 0)
                WfActivityController.CreateActivityController()
                    .GetActivity(Convert.ToInt32(dt.Rows[0]["ACTID"]), Convert.ToInt32(Session["USERID"]));
            return dt;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["USERID"] == null)
            {
                Response.Redirect("login.aspx");
            }
            Utility.RegisterTypeForAjax(GetType(), Page);
        }
    }
}